package com.bailugansan.efficiencyboard.dao.impl;

import com.bailugansan.efficiencyboard.dao.TokenDao;
import com.bailugansan.efficiencyboard.utils.Druid;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author BailuGansan
 */
@Repository
public class TokenDaoImpl implements TokenDao {
    @Override
    public boolean isEffective(String uid, String token) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String sql = "select token from tb_oauth_access_tokens where fr_user_id = ?";
        try {
            // 根据uid获取token字段所有值
            List<Object> list = qr.query(conn, sql, new ColumnListHandler<>(1), uid);
            for(Object o: list){ // 相同则返回true
                if (token.equals(o)){
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return false;
    }
}
